﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YX
{
    class SingleClass
    {
        private SingleClass()
        {

        }


        #region 第一种单例模式
        //private static SingleClass _single=null;
        //public static SingleClass GetSingle
        //{
        //    get
        //    {
        //        if (_single==null)
        //        {
        //            _single = new SingleClass();
        //        }
        //        return _single;
        //    }
        //}
        #endregion

        #region 第二种单例模式

        //public static SingleClass GetSingle { get; } = new SingleClass();
        private static readonly SingleClass singleClass = new SingleClass();
        public static SingleClass GetSingle { get =>singleClass; }
        #endregion

        #region 第三种单例模式
        //public static SingleClass GetSingle
        //{
        //    get
        //    {
        //        return Nested.instance;
        //    }
        //}

        //private class Nested
        //{
        //    static Nested() { }
        //    internal static readonly SingleClass instance = new SingleClass();
        //}
        #endregion

        public BackGround BG
        {
            get;
            set;
        }

        public MyPlane MP
        {
            get;
            set;
        }

        public int Score
        {
            get;
            set;
        }

        public List<MyBullt> MB=new List<MyBullt>();
        public List<EnemyBullt> EB = new List<EnemyBullt>();
        public List<EnemyPlane> EP = new List<EnemyPlane>();
        public List<EnemyBoom> EBoom = new List<EnemyBoom>();
        public List<MyBoom> MBoom = new List<MyBoom>();

        public void AddBaseClase(BaseClass go)
        {
            if(go is BackGround)
            {
                this.BG = go as BackGround;//如果是背景，则放入背景属性
            }
            else if(go is MyPlane)
            {
                this.MP = go as MyPlane;
            }
            else if(go is MyBullt)
            {
               MB.Add(go as MyBullt);
            }
            else if (go is EnemyBullt)
            {
                EB.Add(go as EnemyBullt);
            }
            else if (go is EnemyPlane)
            {
                EP.Add(go as EnemyPlane);
            }
            else if (go is EnemyBoom)
            {
                EBoom.Add(go as EnemyBoom);
            }
            else if (go is MyBoom)
            {
                MBoom.Add(go as MyBoom);
            }
        }

        public void RemoveClass(BaseClass go)
        {
            if(go is MyBullt)
            {
                MB.Remove(go as MyBullt);
            }
            else if (go is EnemyBullt)
            {
                EB.Remove(go as EnemyBullt);
            }
            else if (go is EnemyPlane)
            {
                EP.Remove(go as EnemyPlane);
            }

            else if (go is EnemyBoom)
            {
                EBoom.Remove(go as EnemyBoom);
            }
            else if (go is MyBoom)
            {
                MBoom.Remove(go as MyBoom);
            }
        }

        public void Draw(Graphics g)
        {
            this.BG.Draw(g);
            this.MP.Draw(g);
            for (int i = 0; i < MB.Count; i++)
            {
                this.MB[i].Draw(g);
            }
            for (int i = 0; i < EB.Count; i++)
            {
                this.EB[i].Draw(g);
            }
            for (int i = 0; i < EP.Count; i++)
            {
                this.EP[i].Draw(g);
            }
            for (int i = 0; i < EBoom.Count; i++)
            {
                this.EBoom[i].Draw(g);
            }
            for (int i = 0; i < MBoom.Count; i++)
            {
                this.MBoom[i].Draw(g);
            }
        }

        public void PZJC()
        {
            for(int i=0;i<MB.Count;i++)
            {
                for (int j = 0; j < EP.Count; j++)
                {
                    if(MB[i].GetRectangle().IntersectsWith(EP[j].GetRectangle()))
                    {
                        EP[j].Life -= MB[i].Power;
                        EP[j].It_Over();
                        MB.Remove(MB[i]);//销毁子弹
                        break;
                    }
                }
            }

            for (int j = 0; j < EB.Count; j++)
            {
                if (MP.GetRectangle().IntersectsWith(EB[j].GetRectangle()))
                {
                    SingleClass.GetSingle.AddBaseClase(new MyBoom(MP.X, MP.Y));
                    break;
                }
            }

            for (int j = 0; j < EP.Count; j++)
            {
                if (MP.GetRectangle().IntersectsWith(EP[j].GetRectangle()))
                {
                    EP[j].Life = 0;
                    EP[j].It_Over();
                    break;
                }
            }
        }
    }
}
